﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PIT.Labs.SharePoint.Extensions
{
    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>" for the SPSite class.
    /// </summary>
    /// <seealso cref="T:Microsoft.SharePoint.SPSite"/>
    public static class SPSiteExtension
    {
        /// <summary>Returns an elevated site collection</summary>
        /// <param name="siteCollection">
        /// The siteCollection that you want an elevated instance of.
        /// You must dispose of this object unless it is part of SPContext.Current.*
        /// </param>
        /// <returns><see cref="T:Microsoft.SharePoint.SPSite" /><br />An elevated siteCollection context.</returns>
        /// <remarks>Be sure to dispose of objects created from this method.</remarks>
        /// <example>
        /// This sample shows how to elevate the current Web Site.
        /// <code>
        /// using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
        /// {
        ///     using (SPSite elevatedSite = siteCollection.GetElevatedSite())
        ///     {
        ///         using (SPWeb elevatedWeb = elevatedSite.OpenWeb(SPContext.Current.Web.ID))
        ///         {
        ///         }
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <seealso cref="Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(Microsoft.SharePoint.SPSecurity.CodeToRunElevated)"/>
        [CLSCompliantAttribute(false)]
        public static SPSite GetElevatedSite(this SPSite siteCollection)
        {
            var sysToken = GetSystemToken(siteCollection);
            return new SPSite(siteCollection.ID, sysToken);
        }

        /// <summary>Gets a UserToken for the system account.</summary>
        /// <param name="siteCollection"></param>
        /// <returns>A usertoken for the system account user.</returns>
        /// <remarks>Use this token to impersonate the system account</remarks>
        private static SPUserToken GetSystemToken(SPSite siteCollection)
        {
            siteCollection.CatchAccessDeniedException = false;
            try
            {
                return siteCollection.SystemAccount.UserToken;
            }
            catch (UnauthorizedAccessException)
            {
                SPUserToken sysToken = null;

                //Use RunWithElevatedPrivileges to grab the system user token.
                SPSecurity.RunWithElevatedPrivileges(
                    delegate()
                    {
                        using (SPSite tmpSite = new SPSite(siteCollection.ID))
                        {
                            sysToken = tmpSite.SystemAccount.UserToken;
                        }
                    }
                );
                return sysToken;
            }
        }
    }
}
